package com.tw.modules.web;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.tw.modules.common.ConvertJson;
import com.tw.modules.entity.Diary;
import com.tw.modules.service.DiaryService;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author maxiaodong
 * @since 2018-04-07
 */
@RestController
@RequestMapping("/diary")
public class DiaryController {
	@Autowired
	DiaryService diaryserviceimpl;
	
	/**
	 * 根据手机号和日期查询用户当日信息（测试通过）
	 * @param dphone
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/select", method = { RequestMethod.GET })
	public Diary select(@RequestParam("d_phone")long dphone){
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		return diaryserviceimpl.selectByPhoneAndDate(dphone, df.format(new Date()));
	}
	
	/**
	 * 一个用户从注册累计总番茄个数(测试通过)
	 * 需求分析：根据手机号查询该用户每天的diary表，再将每天的diary表中番茄个数加在一起
	 * @param phone
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectAllTomato", method = { RequestMethod.GET })
	public int selectAllTomato(@RequestParam("uphone")long phone){
		List<Diary> diaries = diaryserviceimpl.selectByPhone(phone);
		Diary diary = new Diary();
		int sumTomato = 0;
		for(int i = 0;i<diaries.size();i++){
			diary = diaries.get(i);
			sumTomato = sumTomato + diary.getdNumber();
		}
		return sumTomato;
	}
	
	/**
	 * 一个用户从注册累计总时间(测试通过)
	 * 需求分析：根据手机号查询该用户每天的diary表，再将每天的diary表中学习时间加在一起
	 * @param phone
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectAllTime", method = { RequestMethod.GET })
	public double selectAllTime(@RequestParam("uphone")long phone){
		List<Diary> diaries = diaryserviceimpl.selectByPhone(phone);
		Diary diary = new Diary();
		double sumDate = 0.00;
		for (int i = 0; i < diaries.size(); i++) {
			diary = diaries.get(i);
			sumDate = sumDate + diary.getdTime();
		}
		return sumDate;
	}
	
	/**
	 * 更新用户diary表(测试通过)
	 * 需求分析：用户每次完成计划(番茄计划不允许停止)都要在diary表中增加一个番茄并增加相应时间;更新表时根据phone和date
	 * @param time
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/update", method = { RequestMethod.GET })
	public String update(@RequestParam("time")double time,@RequestParam("phone")long phone){
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		//根据手机号和日期查询该用户diary表
		Diary diary = diaryserviceimpl.selectByPhoneAndDate(phone, df.format(new Date()));
		System.out.println(diary.getdId());
		Diary diary2 = new Diary();
		diary2.setdId(diary.getdId());
		diary2.setdPhone(phone);
		diary2.setdNumber(diary.getdNumber()+1);
		diary2.setdDate(df.format(new Date()));
		diary2.setdTime(diary.getdTime()+time);
		boolean flag = diaryserviceimpl.updateById(diary2);
		if (flag) {
			return ConvertJson.responseJSON("success");
		}
		return ConvertJson.responseJSON("fail");
	}
}
